import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要扫描的根目录（绝对路径 OR 相对路径): ");
        String rootDirPath = scanner.next();
        File rootDir = new File(rootDirPath);
        if (!rootDir.isDirectory()) {
            System.out.println("您输入的根目录不存在或者不是目录, 退出!");
            return;
        }
        System.out.println("请输入要找出文件名中的字符: ");
        String token = scanner.next();
        List<File> result = new ArrayList<>();
        //递归遍历
        scanDir(rootDir, token, result);
        System.out.println("共找到了符合条件的文件" + result.size() + "个, 他们分别是: ");
        for (File file : result) {
            System.out.println(file.getCanonicalFile() + "请问您是否要删除该文件? y/n");
            String in = scanner.next();
            if (in.equalsIgnoreCase("y")) {
                file.delete();
            }
        }
    }

    private static void scanDir(File rootDir, String token, List<File> result) {
        File[] files = rootDir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                scanDir(file, token, result);
            } else if (file.getName().contains(token)) {
                result.add(file.getAbsoluteFile());
            }
        }
    }
}